import { STORE_OPENID } from '@/api/constant'
import { loadInfo } from '@/api/allapis'
import Lockr from 'Lockr'

const user = {
  state: {
    openid: STORE_OPENID, // 微信openid, 我的openid是 011xMNoH1emrf70CvOrH1IsUoH1xMNog
    uerInfo: Lockr.get('userInfo')
  },
  getters: {
    getOpenid: state => state.openid,
    uerInfo: state => state.uerInfo
  },

  mutations: {
    setOpenid (state, newopenid) {
      state.openid = newopenid
    },
    setUserInfo (state, uerInfo) {
      state.uerInfo = uerInfo
    }
  },

  actions: {
    async GetInfo ({ commit, state }) {
      return new Promise((resolve, reject) => {
        loadInfo({openid: state.openid}).then(response => {
          const data = response.data
          commit('setUserInfo', data)
          Lockr.set('userInfo', data)
          resolve(response)
        }).catch(error => {
          reject(error)
        })
      })
    }
  }
}

export default user
